package com.usermanage.controller;

import com.usermanage.Mapping.GroupMemberMapping;
import com.usermanage.beanParam.GroupMemberBean;
import com.usermanage.util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Optional;

/**
 * 组织管理成员，管理基本权限，管理组织列表
 *searchGroup  查询所有组织类别
 *queryUserGroupByUserId    根据用户id获取当前组
 * queryUserGroupListByUserId   根据用户id得到用户组列表。
 * addGroup   添加组织信息
 */
@Slf4j
@Api(description = "组织和用户关系接口")
@RestController
@RequestMapping("/groupMember")
public class GroupMemberController {

    @Resource
    private GroupMemberMapping groupMemberMapping;

    /**
     * 添加组织信息
     * @param bean
     * @param bindingResult
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "添加组织信息" ,  notes="添加组织信息")
    @RequestMapping(value="/creatGroupMember",method= RequestMethod.POST)
    public JsonData creatGroupMember(@Valid @RequestBody GroupMemberBean bean, BindingResult bindingResult)throws Exception{
        //验证提交参数
        if(bindingResult.hasErrors()){
            return JsonData.fail(bindingResult.getFieldError().getDefaultMessage());
        }

        //验证部门是否存在

        //添加用户和组关联信息

        return JsonData.success();
    }



    @ApiOperation(value = "根据id删除用户组绑定关系" ,  notes="根据id删除用户组绑定关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户和组关联id", required = true,  paramType = "query", dataType = "String")
    })
    @RequestMapping(value="/deleteGroupMember",method= RequestMethod.POST)
    public JsonData deleteGroupMember(@RequestParam("id") String id)throws Exception{
        return JsonData.success();
    }


    @ApiOperation(value = "移除某组织下的员工" ,  notes="移除某组织下的员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户标识", required = true,  paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "groupId", value = "组标识", required = true,  paramType = "query", dataType = "String")
    })
    @RequestMapping(value="/removeUserFromGroup",method= RequestMethod.GET)
    public JsonData removeUserFromGroup(@RequestParam("userId") String userId,@RequestParam("groupId") String groupId)throws Exception{
        //根据用户id得到用户详细信息
        GroupMemberBean bean = new GroupMemberBean();
        bean.setUser_id(userId);
        bean.setGroup_id(groupId);
//        bean.setTop_group_id("1");
        Example<GroupMemberBean> example = Example.of(bean);
        Optional<GroupMemberBean> optional = groupMemberMapping.findOne(example);
        if (optional.isPresent()){
            groupMemberMapping.deleteById(optional.get().getId());
        }
        //返回用户信息
        return JsonData.success();
    }

}
